package com.xiaoha.reading.exception;

import org.springframework.stereotype.Controller;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;

/**
 * WebExceptionHandel
 *
 * @author: cyx_jay
 * @date: 2021/3/5 21:27
 */
@ControllerAdvice
public class WebExceptionHandel {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String handle(Exception e){
        if (e instanceof MethodArgumentNotValidException){
            ObjectError objectError = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors().get(0);
            String defaultMessage = objectError.getDefaultMessage();
            return defaultMessage;
        }

        return "hello slm1";
    }
}
